package com.zhou.yygh.cmn.controller;

import com.zhou.yygh.cmn.service.DictService;
import com.zhou.yygh.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import zhou.hospital.model.cmn.Dict;

import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Api(tags = "数据字典管理")
@RestController
@RequestMapping(value = "/admin/cmn/dict")

public class DictController {
    @Autowired
    private DictService dictService;

    @ApiOperation(value = "根据Id查找子数据列表")
    @GetMapping("/findChildDate/{id}")
    public Result findChildDate(@PathVariable Long id) {
        List<Dict> list = dictService.findChildDate(id);
        return Result.ok(list);
    }


    @ApiOperation(value = "导出数据字典")
    @GetMapping("/exportData")
    public Result exportDict(HttpServletResponse response) {
        dictService.exportDictDate(response);
        return Result.ok();
    }


    @ApiOperation(value = "导入数据字典")
    @PostMapping("/importData")
    public Result importDict(MultipartFile file) {
        dictService.importDictData(file);
        return Result.ok();

    }


    @ApiOperation(value = "根据value和dict_code查询 name")
    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(@PathVariable String dictCode,
                          @PathVariable String value) {
        String dictName = dictService.getDictName(dictCode, value);
        return dictName;
    }


    @ApiOperation(value = "根据 value 查询 name")
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable String value) {
        String dictName = dictService.getDictName("",value);

        return dictName;
    }

    @ApiOperation(value = "根据dict_code查询所有的子节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable String dictCode){
       List<Dict> dictList= dictService.findByDictCode(dictCode);
       return Result.ok(dictList);

    }

}
